<template>
	<view class="form-wrap">
		<view>
			<view class="label"><text class="request">*</text>物料编码</view>
			<view class="input-wrap">
				<input v-model="form.productNo" @input="getProduct" class="input" type="text" placeholder="请扫描/输入物料编码" />
				<icon v-if="form.productNo" type="clear" size="26" @click="clearForm" style="margin-right: 20rpx;"/>
			</view>
		</view>
		<view>
			<view class="label"><text class="request">*</text>物料名称</view>
			<view class="input-wrap">
				<input v-model="form.productName" disabled="" class="input" type="text" placeholder="物料名称" />
			</view>
		</view>
		<view>
			<view class="label"><text class="request">*</text>物料数量</view>
			<view class="input-wrap">
				<input v-model="form.quantity" class="input" type="number" @input="setNum" placeholder="请输入数量" />
			</view>
		</view>
		<view>
			<view class="label">订单编号</view>
			<view class="input-wrap">
				<input v-model="form.orderNo" class="input" type="text" placeholder="请扫描/输入订单编号" />
			</view>
		</view>
        <button @click="submit" class="submit-btn">确定</button>
	</view>
</template>

<script>
	import { receipt, getProduct } from "./api"
	var printModule = uni.requireNativePlugin("pda-print-PdaPrintModule")
	
	export default {
		data() {
			return {
				form:{
					productNo:'',
					productName:'',
					quantity:'',
					orderNo:''
				}
			}
		},
		methods:{
			clearForm(){
				this.form = {
					productNo:'',
					productName:'',
					quantity:'',
					orderNo:''
				}
			},
			getProduct(e) {
				if (e.target.value === "") {
					this.$modal.msg("请输入物料编号")
					this.clearForm()
				}else{
					getProduct({productNo:e.target.value}).then(res => {
						if (res.code == 200) {
							let form = {...res.rows[0], quantity:''}
							this.form = form
						}
					})
				}
			},
			// 验证大于等于1的整数
			setNum(e){
				if(e.target.value){
					let newValue = e.target.value.replace(/^\D*(\d*).*$/g, "$1")
					newValue = Math.max(1,newValue)
					this.$nextTick(()=>{
						this.form.quantity = newValue
					})
				}
			},
			submit(){
				if (!this.form.productNo ) {
				  this.$modal.msg("请输入物料编码")
				} else if (!this.form.quantity  || this.form.quantity ==0 ) {
				  this.$modal.msg("请输入数量")
				} else{
					this.$modal.loading("收货中...")
					receipt(this.form).then(res=>{
						this.$modal.closeLoading()
						console.log(res)
						if(res.code == 200){
							this.$modal.msgSuccess("收货成功")
							printModule.printLabelAsync({
								'barcode': res.data.batchNo,
								'title': res.data.batchNo + '\n\n物料编号：'+ this.form.productNo + '\n数量：'+this.form.quantity,
								'distance': 500,
							},(ret) => {
								console.log(ret);
							});
							this.form = {
								productNo:'',
								quantity:''
							}
						}
					})
				}
			},
		}
	}
</script>

<style>
</style>
